import os

import pytest
from errors import Duplicate, Missing
from model.creature import Creature

# set this before data imports below for data.init
os.environ["CRYPTID_SQLITE_DB"] = ":memory:"
from data import creature


@pytest.fixture
def sample() -> Creature:
    return Creature(
        name="New Yetti",
        country="CN",
        area="Himalayas",
        description="Hirsute Himalayan",
        aka="Abominable Snowman",
    )


#
# These tests will be executed sequentially in the same order each time.
#
def test_create(sample):
    resp = creature.create(sample)
    assert resp == sample


def test_create_duplicate(sample):
    with pytest.raises(Duplicate):
        creature.create(sample)


def test_get_one(sample):
    resp = creature.get_one(sample.name)
    assert resp == sample


def test_get_one_missing():
    with pytest.raises(Missing):
        creature.get_one("hazadus")


def test_modify(sample):
    modified = Creature(
        name=sample.name,
        country=sample.country,
        area=sample.area,
        description=sample.description,
        aka=sample.aka,
    )
    modified.aka = "Hazadus"
    resp = creature.modify(sample.name, modified)
    assert resp == modified


def test_modify_missing():
    thing: Creature = Creature(
        name="Баба Яга",
        country="RU",
        area="Лес",
        description="Злая",
        aka="",
    )
    with pytest.raises(Missing):
        creature.modify(thing.name, thing)


def test_delete(sample):
    resp = creature.delete(sample.name)
    assert resp is None


def test_delete_missing(sample):
    with pytest.raises(Missing):
        creature.delete(sample.name)
